#include "LevelBuilder.h"

#include <gccore.h>
#include <wiiuse/wpad.h>

extern u8 start_png[];
extern u8 cake_png[];
extern u8 transGround_png[];
extern u8 ground_png[];
extern u8 nonPortalWall_png[];
extern u8 delete_png[];

#define OFFX -31
#define OFFY -23

LevelBuilder::LevelBuilder(SoundManager* ptrSndManager)
{
		_ptrSndManager = ptrSndManager;
        for(int i = 0; i < Builder::SIZE; i++)
        {
                images.push_back(new Image());
        }
	
        if(images[Builder::START]->LoadImage(start_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
        if(images[Builder::CAKE]->LoadImage(cake_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
        if(images[Builder::GROUND]->LoadImage(ground_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
        if(images[Builder::TRANS_GROUND]->LoadImage(transGround_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
        if(images[Builder::NON_PORTAL_WALL]->LoadImage(nonPortalWall_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
        if(images[Builder::DELETE]->LoadImage(delete_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
	
	//_npWallSprite.SetImage(images[Builder::NON_PORTAL_WALL]);
	
        m_world = NULL;
		

        Init();
        _saveIndex = 0;
		_unsavedChanges = false;
		
}

LevelBuilder::~LevelBuilder()
{

}
	
/* my save callback */
const char *
whitespace_cb(mxml_node_t *node,
			  int where)
{
	const char *name;

	name = node->value.element.name;

	if (!strcmp(name, "portii"))
	{
			return ("\n");
	}
	else if (!strcmp(name, "ground") ||
			 !strcmp(name, "transground") ||
			 !strcmp(name, "startPosition") ||
			 !strcmp(name, "cake") ||
			 !strcmp(name, "nonPortalWall"))
	{
		if (where == MXML_WS_BEFORE_OPEN)
			return ("\t");
		else if (where == MXML_WS_AFTER_OPEN)
			return ("\n");
		if (where == MXML_WS_BEFORE_CLOSE)
			return ("\t");
		else if (where == MXML_WS_AFTER_CLOSE)
			return ("\n");
	}
	
	return (NULL);
}

void LevelBuilder::Init()
{
       m_isDrawing = false;

       m_startSet = false;
	
       m_currentType = Builder::START;

       m_drawSprite.SetImage(images[m_currentType]);

       m_screenOffsetX = 0;
       m_screenOffsetY = 0;
	
	ClearAll();

       m_world = NULL;
}

void LevelBuilder::ClearAll()
{
        for(int i = 0; i < (int)m_cakeSprites.size(); i++)
               if(m_cakeSprites[i])
                 delete m_cakeSprites[i];

        for(int i = 0; i < (int)m_groundSprites.size(); i++)
               if(m_groundSprites[i])
                 delete m_groundSprites[i];

        for(int i = 0; i < (int)m_transGroundSprites.size(); i++)
               if(m_transGroundSprites[i])
                 delete m_transGroundSprites[i];

        for(int i = 0; i < (int)m_nonPortalWallSprites.size(); i++)
               if(m_nonPortalWallSprites[i])
                 delete m_nonPortalWallSprites[i];

        for(int i = 0; i < (int)_storyText.size(); i++)
               if(_storyText[i])
                 delete _storyText[i];

       m_cakeSprites.clear();
       m_groundSprites.clear();
       m_transGroundSprites.clear();
       m_nonPortalWallSprites.clear();
       _storyText.clear();
	
        CleanPhysics();
}

bool LevelBuilder::Save(char* filename)
{
    mxml_node_t *xml;
	mxml_node_t *tree;
	mxml_node_t *node;
	
    char buffer[50];
	
	xml = mxmlNewXML("1.0");
	
	tree = mxmlNewElement(xml, "portii");
	
	for(int i=0; i<(int)m_cakeSprites.size(); ++i)
	{
		node = mxmlNewElement(tree, "cake");
		
		sprintf(buffer, "%f", m_cakeSprites[i]->GetX());
		mxmlElementSetAttr(node, "x", buffer);
		
		sprintf(buffer, "%f", m_cakeSprites[i]->GetY());
		mxmlElementSetAttr(node, "y", buffer);
	}
	
	for(int i=0; i<(int)m_groundSprites.size(); ++i)
	{
		node = mxmlNewElement(tree, "ground");
		
		sprintf(buffer, "%f", m_groundSprites[i]->GetX());
		mxmlElementSetAttr(node, "x", buffer);
		
		sprintf(buffer, "%f", m_groundSprites[i]->GetY());
		mxmlElementSetAttr(node, "y", buffer);
		
		sprintf(buffer, "%f", m_groundSprites[i]->GetRotation());
		mxmlElementSetAttr(node, "rotation", buffer);
	}
	
	for(int i=0; i<(int)m_transGroundSprites.size(); ++i)
	{
		node = mxmlNewElement(tree, "transground");
		
		sprintf(buffer, "%f", m_transGroundSprites[i]->GetX());
		mxmlElementSetAttr(node, "x", buffer);
		
		sprintf(buffer, "%f", m_transGroundSprites[i]->GetY());
		mxmlElementSetAttr(node, "y", buffer);
		
		sprintf(buffer, "%f", m_transGroundSprites[i]->GetRotation());
		mxmlElementSetAttr(node, "rotation", buffer);
	}
	
	for(int i=0; i<(int)m_nonPortalWallSprites.size(); ++i)
	{
		node = mxmlNewElement(tree, "nonPortalWall");
		
		sprintf(buffer, "%f", m_nonPortalWallSprites[i]->GetX());
		mxmlElementSetAttr(node, "x", buffer);
		
		sprintf(buffer, "%f", m_nonPortalWallSprites[i]->GetY());
		mxmlElementSetAttr(node, "y", buffer);
		
		sprintf(buffer, "%f", (float)m_nonPortalWallSprites[i]->GetWidth());
		mxmlElementSetAttr(node, "width", buffer);
		
		sprintf(buffer, "%f", (float)m_nonPortalWallSprites[i]->GetHeight());
		mxmlElementSetAttr(node, "height", buffer);
	}

	if(m_startSet)
	{
		node = mxmlNewElement(tree, "startPosition");
		
		sprintf(buffer, "%f", m_startSprite.GetX());
		mxmlElementSetAttr(node, "x", buffer);
		
		sprintf(buffer, "%f", m_startSprite.GetY());
		mxmlElementSetAttr(node, "y", buffer);
		
		sprintf(buffer, "%f", m_startSprite.GetRotation());
		mxmlElementSetAttr(node, "rotation", buffer);
	}

        //Tmp save for story building
        /*
	FILE *fpt;
	char fname[200];
        sprintf(fname, "story_%d.portii", _saveIndex);
        _saveIndex++;
	fpt = fopen(fname, "w");
	
	if(fpt != NULL)
	{
		mxmlSaveFile(xml, fpt, whitespace_cb);
	
		fclose(fpt);
	}
        */

	FILE *fp;

        bool append =true;
        
        if(strlen(filename)>7)
        {
                if(filename[strlen(filename) - 7] == '.' &&
                        filename[strlen(filename) - 6] == 'p' &&
                        filename[strlen(filename) - 5] == 'o' &&
                        filename[strlen(filename) - 4] == 'r' &&
                        filename[strlen(filename) - 3] == 't' &&
                        filename[strlen(filename) - 2] == 'i' &&
                        filename[strlen(filename) - 1] == 'i')
                {
                        char buf[200];
                        sprintf(buf, "/apps/portii/%s", filename);
                        fp = fopen(buf, "w");
                        append = false;
                }
                
        }

        if(append)
        {
                char buf[200];
                sprintf(buf, "/apps/portii/%s.portii", filename);
                fp = fopen(buf, "w");
        }
        
	if(fp != NULL)
	{
		mxmlSaveFile(xml, fp, whitespace_cb);
	
		fclose(fp);
	
		_unsavedChanges = false;
		return true;
	}
	else
	{
		return false;
	}
}

bool LevelBuilder::LoadFromFile(char* filename)
{
  FILE *fp;
  mxml_node_t *xml;
  
  
  char buff[200];
  
  sprintf(buff, "/apps/portii/%s",filename);
  
  fp = fopen(buff, "r");
  
  xml = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK);
  fclose(fp);
  
  Init();
  
  _unsavedChanges = false;
		
  if(xml != NULL)
	return ParseTree(xml);
  else
	return false;
}

bool LevelBuilder::LoadFromBuffer(char* buffer)
{
  mxml_node_t *xml;
  xml = mxmlLoadString(NULL, buffer, MXML_NO_CALLBACK);
  
  Init();
  
  if(xml != NULL)
	return ParseTree(xml);
  else
	return false;
}

bool LevelBuilder::ParseTree(mxml_node_t *tree)
{
  mxml_node_t *node;
  mxml_node_t *next_n;
	
  node = mxmlFindElement(tree,tree, "portii", NULL, NULL, MXML_DESCEND); 
  if(node == NULL) return false;
  
  //Add cake
  for(next_n = mxmlFindElement(node, node, "cake", NULL, NULL, MXML_DESCEND); 
        next_n != NULL; 
        next_n = mxmlFindElement(next_n, node, "cake", NULL, NULL, MXML_DESCEND))
  {
	 float x = atof(mxmlElementGetAttr(next_n,"x"));
	 float y = atof(mxmlElementGetAttr(next_n,"y"));

	 int index = m_cakeSprites.size();

	 m_cakeSprites.push_back(new Sprite());
	 m_cakeSprites[index]->SetImage(images[Builder::CAKE]);
	 m_cakeSprites[index]->SetPosition(x, y);
  }
  
  //Add ground
  for(next_n = mxmlFindElement(node, node, "ground", NULL, NULL, MXML_DESCEND); 
        next_n != NULL; 
        next_n = mxmlFindElement(next_n, node, "ground", NULL, NULL, MXML_DESCEND))
  {
	 float x = atof(mxmlElementGetAttr(next_n,"x"));
	 float y = atof(mxmlElementGetAttr(next_n,"y"));
	 float rotation = atof(mxmlElementGetAttr(next_n,"rotation"));

	 int index = m_groundSprites.size();

	 m_groundSprites.push_back(new Sprite());
	 m_groundSprites[index]->SetImage(images[Builder::GROUND]);
	 m_groundSprites[index]->SetPosition(x, y);
	 m_groundSprites[index]->SetRotation(rotation);
  }
  
  //Add trans ground
  for(next_n = mxmlFindElement(node, node, "transground", NULL, NULL, MXML_DESCEND); 
        next_n != NULL; 
        next_n = mxmlFindElement(next_n, node, "transground", NULL, NULL, MXML_DESCEND))
  {
	 float x = atof(mxmlElementGetAttr(next_n,"x"));
	 float y = atof(mxmlElementGetAttr(next_n,"y"));
	 float rotation = atof(mxmlElementGetAttr(next_n,"rotation"));

	 int index = m_transGroundSprites.size();

	 m_transGroundSprites.push_back(new Sprite());
	 m_transGroundSprites[index]->SetImage(images[Builder::TRANS_GROUND]);
	 m_transGroundSprites[index]->SetPosition(x, y);
	 m_transGroundSprites[index]->SetRotation(rotation);
  }
  
  //Add non portal walls
  for(next_n = mxmlFindElement(node, node, "nonPortalWall", NULL, NULL, MXML_DESCEND); 
        next_n != NULL; 
        next_n = mxmlFindElement(next_n, node, "nonPortalWall", NULL, NULL, MXML_DESCEND))
  {
	 float x = atof(mxmlElementGetAttr(next_n,"x"));
	 float y = atof(mxmlElementGetAttr(next_n,"y"));
	 float width = atof(mxmlElementGetAttr(next_n,"width"));
	 float height = atof(mxmlElementGetAttr(next_n,"height"));

	 int index = m_nonPortalWallSprites.size();

	 m_nonPortalWallSprites.push_back(new Quad());
	 m_nonPortalWallSprites[index]->SetPosition(x, y);
	 m_nonPortalWallSprites[index]->SetWidth(width);
	 m_nonPortalWallSprites[index]->SetHeight(height);
	 
  }
  
  for(next_n = mxmlFindElement(node, node, "startPosition", NULL, NULL, MXML_DESCEND); 
        next_n != NULL; 
        next_n = mxmlFindElement(next_n, node, "startPosition", NULL, NULL, MXML_DESCEND))
  {
	 float x = atof(mxmlElementGetAttr(next_n,"x"));
	 float y = atof(mxmlElementGetAttr(next_n,"y"));
	 float rotation = atof(mxmlElementGetAttr(next_n,"rotation"));
	 
	 m_startSet = true;
	
     m_startSprite.SetPosition(x, y);
     m_startSprite.SetRotation(rotation);
     m_startSprite.SetImage(images[Builder::START]); 
  }

  for(next_n = mxmlFindElement(node, node, "story", NULL, NULL, MXML_DESCEND); 
        next_n != NULL; 
        next_n = mxmlFindElement(next_n, node, "story", NULL, NULL, MXML_DESCEND))
  {
	 _storyText.push_back(new StoryData(mxmlElementGetAttr(next_n, "text"),mxmlElementGetAttr(next_n, "text2")));
  }

  return true;
}

bool LevelBuilder::Update()
{
	WPAD_ScanPads();
		
        m_isRunning = true;

	u16 btnsheld = WPAD_ButtonsHeld(0);
	u16 btnsdown = WPAD_ButtonsDown(0);
	
        ir_t ir; // The struct for infrared
        
        WPAD_IR(WPAD_CHAN_0, &ir); // Let's get our infrared data

        m_drawSprite.SetPosition(ir.sx-200, ir.sy-250);
        m_drawSprite.SetRotation(ir.angle/2); // Set angle/2 to translate correctly

	if(btnsdown & WPAD_BUTTON_HOME)
	{
                //TODO Bring up menu letting user choose between Save, Quit and Test
                m_isRunning = false;
		return false;
	}
	
	if(btnsheld & WPAD_BUTTON_LEFT)
	{
            m_screenOffsetX -= 3;
	}

	if(btnsheld & WPAD_BUTTON_RIGHT)
	{
            m_screenOffsetX += 3;
	}

	if(btnsheld & WPAD_BUTTON_UP)
	{
            m_screenOffsetY -= 3;
	}

	if(btnsheld & WPAD_BUTTON_DOWN)
	{
            m_screenOffsetY += 3;
	}
	
	if(btnsdown & WPAD_BUTTON_PLUS && !m_isDrawing)
	{
		_ptrSndManager->PlaySound(Sound::BLOOP_FX, 0);
	    m_currentType = Builder::Type(m_currentType + 1);

            if(m_currentType == Builder::SIZE)
            {
               m_currentType = Builder::START;
            }
            m_drawSprite.SetImage(images[m_currentType]);
	}

	if(btnsdown & WPAD_BUTTON_MINUS && !m_isDrawing)
	{
		_ptrSndManager->PlaySound(Sound::BLOOP_FX, 0);
            if(m_currentType == Builder::START)
            {
               m_currentType = Builder::DELETE;
            }
            else
            {
                m_currentType = Builder::Type(m_currentType - 1);;
            }
            m_drawSprite.SetImage(images[m_currentType]);
	}

        
	if(btnsdown & WPAD_BUTTON_A)
	{
                m_downA_X = m_drawSprite.GetX();
                m_downA_Y = m_drawSprite.GetY();

                if(m_currentType == Builder::START ||
                   m_currentType == Builder::CAKE  ||
                   m_currentType == Builder::TRANS_GROUND  ||
                   m_currentType == Builder::GROUND ||
                   m_currentType == Builder::DELETE)
                {
		        //Add Current Type
                        AddElement();
                }
                else
                {
                        m_isDrawing = true;
                }
                
	}
        
        if(!(btnsheld & WPAD_BUTTON_A))
        {
                if(m_isDrawing)
                {
                        AddElement();
                }
                m_isDrawing = false;
        }
        
        return true;
}

void LevelBuilder::AddElement()
{
        switch(m_currentType)
        {
                case Builder::START:
						_unsavedChanges = true;
						_ptrSndManager->PlaySound(Sound::CREATE_FX, 0);
                        m_startSprite.SetPosition(m_drawSprite.GetX() + m_screenOffsetX, m_drawSprite.GetY() + m_screenOffsetY);
                        m_startSprite.SetRotation(m_drawSprite.GetRotation());
                        m_startSprite.SetImage(images[Builder::START]);
                        m_startSet = true;
                        break;
                case Builder::CAKE:
                        if((int)m_cakeSprites.size() < MAX_CAKE_SLICES)
                        {
								_unsavedChanges = true;
							_ptrSndManager->PlaySound(Sound::CREATE_FX, 0);
                                int index = m_cakeSprites.size();
                                m_cakeSprites.push_back(new Sprite());
                                m_cakeSprites[index]->SetPosition(m_drawSprite.GetX() + m_screenOffsetX, m_drawSprite.GetY() + m_screenOffsetY);
                                m_cakeSprites[index]->SetRotation(m_drawSprite.GetRotation());
                                m_cakeSprites[index]->SetImage(images[Builder::CAKE]);
                        }
                        break;
                case Builder::TRANS_GROUND:
                        {
								_unsavedChanges = true;
							_ptrSndManager->PlaySound(Sound::CREATE_FX, 0);
                                int index = m_transGroundSprites.size();
                                m_transGroundSprites.push_back(new Sprite());
                                m_transGroundSprites[index]->SetPosition(m_drawSprite.GetX() + m_screenOffsetX, m_drawSprite.GetY() + m_screenOffsetY);
                                m_transGroundSprites[index]->SetRotation(m_drawSprite.GetRotation());
                                m_transGroundSprites[index]->SetImage(images[Builder::TRANS_GROUND]);
                        }
                        break;
                case Builder::GROUND:
                        {
								_unsavedChanges = true;
							_ptrSndManager->PlaySound(Sound::CREATE_FX, 0);
                                int index = m_groundSprites.size();
                                m_groundSprites.push_back(new Sprite());
                                m_groundSprites[index]->SetPosition(m_drawSprite.GetX() + m_screenOffsetX, m_drawSprite.GetY() + m_screenOffsetY);
                                m_groundSprites[index]->SetRotation(m_drawSprite.GetRotation());
                                m_groundSprites[index]->SetImage(images[Builder::GROUND]);
                        }
                        break;
                case Builder::NON_PORTAL_WALL:
                        {
								_unsavedChanges = true;
							_ptrSndManager->PlaySound(Sound::CREATE_FX, 0);
                                int index = m_nonPortalWallSprites.size();
                                m_nonPortalWallSprites.push_back(new Quad());
                                ResizeQuad(m_nonPortalWallSprites[index]);
                                m_nonPortalWallSprites[index]->SetPosition(m_nonPortalWallSprites[index]->GetX() + m_screenOffsetX,
                                                                          m_nonPortalWallSprites[index]->GetY() + m_screenOffsetY);
                                m_nonPortalWallSprites[index]->SetFillColor((GXColor){0, 0, 0, 255 });
                        }
                        break;
                case Builder::DELETE:
						_ptrSndManager->PlaySound(Sound::DELETE_FX, 0);
                        DeleteSelected();
                        break;
                default:
                        break;
        }
}

void LevelBuilder::DeleteSelected()
{
        Sprite tSprite;
        tSprite.SetPosition(m_drawSprite.GetX() + m_screenOffsetX, m_drawSprite.GetY() + m_screenOffsetY);
        tSprite.SetImage(images[Builder::DELETE]);

        for(int i = 0; i < (int)m_nonPortalWallSprites.size(); i++)
        {
                if(tSprite.CollidesWith(m_nonPortalWallSprites[i]->GetRectangle()))
				{
						_unsavedChanges = true;
                        m_nonPortalWallSprites.erase(m_nonPortalWallSprites.begin()+(i));
				}
        }

        for(int i = 0; i < (int)m_groundSprites.size(); i++)
        {
                if(tSprite.CollidesWith(m_groundSprites[i], true))
				{
						_unsavedChanges = true;
                        m_groundSprites.erase(m_groundSprites.begin()+(i));
				}
        }

        for(int i = 0; i < (int)m_transGroundSprites.size(); i++)
        {
                if(tSprite.CollidesWith(m_transGroundSprites[i], true))
				{
						_unsavedChanges = true;
                        m_transGroundSprites.erase(m_transGroundSprites.begin()+(i));
				}
        }

        for(int i = 0; i < (int)m_cakeSprites.size(); i++)
        {
                if(tSprite.CollidesWith(m_cakeSprites[i], true))
				{
						_unsavedChanges = true;
                        m_cakeSprites.erase(m_cakeSprites.begin()+(i));
				}
        }

        if(m_startSet)
        {
                if(tSprite.CollidesWith(&m_startSprite, true))
				{
						_unsavedChanges = true;
                        m_startSet = false;
				}
        }

}

void LevelBuilder::ResizeQuad(Quad* q)
{
    float x = m_drawSprite.GetX();
    float y = m_drawSprite.GetY();

    if(x<0.0) x=0.0;
    if(y<0.0) y=0.0;
    s32 dw=(s32)(x-m_downA_X);
    s32 dh=(s32)(y-m_downA_Y);
    u32 w;
    u32 h;
    u32 nx;
    u32 ny;
    
    if(dw<0){
        dw=-dw;
        nx=x;
    }else{
        nx=m_downA_X;
    }
    w=(u32)dw;
    if(w<10) w=10;
    
    if(dh<0){
        dh=-dh;
        ny=y;
    }else{
        ny=m_downA_Y;
    }
    h=(u32)dh;
    if(h<10) h=10;
    
    q->SetX(nx);
    q->SetY(ny);
    q->SetWidth(w);
    q->SetHeight(h);
}

void LevelBuilder::CleanPhysics()
{
	if(m_world != NULL)
	{
		for(int i = 0; i < (int)m_bodies.size(); i++)
		{
		       m_world->DestroyBody(m_bodies[i]);
		}
	       m_bodies.clear();
	}
}

void LevelBuilder::Phyisicize(b2World* w)
{
       m_world = w;

        m_screenOffsetX = 0;
        m_screenOffsetY = 0;

        for(int i = 0; i < (int)m_transGroundSprites.size(); i++)
        {
                b2BodyDef bodyDef;
                
                bodyDef.position.Set((m_transGroundSprites[i]->GetX()+OFFX+m_transGroundSprites[i]->GetWidth()/2.0) / PIXELS_PER_UNIT, (m_transGroundSprites[i]->GetY()+OFFY+m_transGroundSprites[i]->GetHeight()/2.0) / PIXELS_PER_UNIT);
                        
                bodyDef.angle = (m_transGroundSprites[i]->GetRotation()*2.0) * ( M_PI / 180.0);
                b2Body* body = w->CreateBody(&bodyDef);
                
                b2PolygonDef polydef;
                polydef.SetAsBox(((m_transGroundSprites[i]->GetWidth()/PIXELS_PER_UNIT)/2.0), ((m_transGroundSprites[i]->GetHeight()/PIXELS_PER_UNIT)/2.0));
                
			       m_bodies.push_back(body);

                polydef.density = DEFAULT_DENSITY;
                
                polydef.friction = DEFAULT_FRICTION;
                polydef.restitution = DEFAULT_RESTITUTION;
                
                body->CreateShape(&polydef);	
        }

        for(int i = 0; i < (int)m_groundSprites.size(); i++)
        {
                b2BodyDef bodyDef;
                
                bodyDef.position.Set((m_groundSprites[i]->GetX()+OFFX+m_groundSprites[i]->GetWidth()/2.0) / PIXELS_PER_UNIT, (m_groundSprites[i]->GetY()+OFFY+m_groundSprites[i]->GetHeight()/2.0) / PIXELS_PER_UNIT);
                        
                bodyDef.angle = (m_groundSprites[i]->GetRotation()*2.0) * ( M_PI / 180.0);
                b2Body* body = w->CreateBody(&bodyDef);
                
                b2PolygonDef polydef;
                polydef.SetAsBox(((m_groundSprites[i]->GetWidth()/PIXELS_PER_UNIT)/2.0), ((m_groundSprites[i]->GetHeight()/PIXELS_PER_UNIT)/2.0));
                
			       m_bodies.push_back(body);

                polydef.density = DEFAULT_DENSITY;
                
                polydef.friction = DEFAULT_FRICTION;
                polydef.restitution = DEFAULT_RESTITUTION;
                
                body->CreateShape(&polydef);	
        }   
}

void LevelBuilder::Render(float dx, float dy, bool in_game)
{
        if(m_isDrawing && Builder::NON_PORTAL_WALL)
        {
                Quad t_q;
                t_q.SetFillColor((GXColor){ 0, 0, 0, 255 });
                ResizeQuad(&t_q);
                t_q.Draw();
        }

        for(int i = 0; i < (int)m_nonPortalWallSprites.size(); i++)
        {
                m_nonPortalWallSprites[i]->Draw(-1*m_screenOffsetX + dx, -1*m_screenOffsetY + dy);
        }

        for(int i = 0; i < (int)m_transGroundSprites.size(); i++)
        {
                m_transGroundSprites[i]->Draw(-1*m_screenOffsetX + dx, -1*m_screenOffsetY+ dy);
        }

        for(int i = 0; i < (int)m_groundSprites.size(); i++)
        {
                m_groundSprites[i]->Draw(-1*m_screenOffsetX + dx, -1*m_screenOffsetY+ dy);
        }


		if(!in_game)
		{
			for(int i = 0; i < (int)m_cakeSprites.size(); i++)
			{
			       m_cakeSprites[i]->Draw(-1*m_screenOffsetX + dx, -1*m_screenOffsetY+ dy);
			}

			if(m_startSet)
			{
			       m_startSprite.Draw(-1*m_screenOffsetX + dx, -1*m_screenOffsetY+ dy);
			}
			
        	if(m_isRunning)
                m_drawSprite.Draw();
		}
		

}

void LevelBuilder::ConstructCakes(std::vector<Cake*> *cakeVector)
{
        for(int i = 0; i < (int)m_cakeSprites.size(); i++)
        {
				cakeVector->push_back(new Cake(images[Builder::CAKE]));
                (cakeVector->at(i))->SetLocation(m_cakeSprites[i]->GetX(), m_cakeSprites[i]->GetY());
        }
}

void LevelBuilder::ConstructNpZones(std::vector<Quad*> *npZones)
{
        for(int i = 0; i < (int)m_nonPortalWallSprites.size(); i++)
        {
				npZones->push_back(new Quad());
				(npZones->at(i))->SetRectangle((Rectangle*)m_nonPortalWallSprites[i]->GetRectangle());
        }
}

void LevelBuilder::ConstructGround(std::vector<Sprite*> *ground)
{
        for(int i = 0; i < (int)m_groundSprites.size(); i++)
        {
				ground->push_back(new Sprite());
				(ground->at(i))->SetImage(images[Builder::GROUND]);
				(ground->at(i))->SetPosition(m_groundSprites[i]->GetX(), m_groundSprites[i]->GetY());
				(ground->at(i))->SetRotation(m_groundSprites[i]->GetRotation());
        }
}

void LevelBuilder::ConstructTransGround(std::vector<Sprite*> *transGround)
{
        for(int i = 0; i < (int)m_transGroundSprites.size(); i++)
        {
				transGround->push_back(new Sprite());
				(transGround->at(i))->SetImage(images[Builder::GROUND]);
				(transGround->at(i))->SetPosition(m_transGroundSprites[i]->GetX(), m_transGroundSprites[i]->GetY());
				(transGround->at(i))->SetRotation(m_transGroundSprites[i]->GetRotation());
        }
}

void LevelBuilder::ConstructStoryText(std::vector<StoryData*> *storyText)
{
		/* Test load story data */
		//storyText->push_back("This is a test....\n \nDid it work?");
		//storyText->push_back("Good! Looks like it did!");
		
		/*End Test*/

        for(int i = 0; i < (int)_storyText.size(); i++)
        {
		storyText->push_back(new StoryData(_storyText[i]->GetData(), _storyText[i]->GetData2()));
        }
}
